package com.donfelix.looknews.view.implpresenter;

import android.content.Context;

import com.donfelix.looknews.api.ApiManager;
import com.donfelix.looknews.bean.zhihu.ZhihuDaily;
import com.donfelix.looknews.bean.zhihu.ZhihuDailyItem;
import com.donfelix.looknews.common.Config;
import com.donfelix.looknews.util.CacheUtil;
import com.donfelix.looknews.view.implview.IZhihuFragment;
import com.donfelix.looknews.view.implview.IZhihuPresenter;
import com.google.gson.Gson;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Don on 2017/3/29.
 * Desc
 */

public class ZhihuPresenterImpl extends BasePresenterImpl implements IZhihuPresenter {

    private IZhihuFragment zhihuFragment;
    private CacheUtil cacheUtil;
    private Gson gson;

    public ZhihuPresenterImpl(Context context, IZhihuFragment zhihuFragment) {
        this.zhihuFragment = zhihuFragment;
        cacheUtil = CacheUtil.get(context);
        gson = new Gson();
    }

    @Override
    public void getLatestZhihuNews() {
        zhihuFragment.showProgressDialog();
        ApiManager.getInstance().getZhihuApiService().getLatestDaily()
                .map(new Function<ZhihuDaily, ZhihuDaily>() {
                    @Override
                    public ZhihuDaily apply(@NonNull ZhihuDaily daily) throws Exception {
                        String date = daily.getDate();
                        for (ZhihuDailyItem item : daily.getStories()) {
                            item.setDate(date);
                        }
                        return daily;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ZhihuDaily>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(ZhihuDaily daily) {
                        zhihuFragment.hideProgressDialog();
                        cacheUtil.put(Config.ZHIHU, gson.toJson(daily));
                        zhihuFragment.updateList(daily);
                    }

                    @Override
                    public void onError(Throwable e) {
                        zhihuFragment.hideProgressDialog();
                        zhihuFragment.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @Override
    public void getTheDauly(String date) {
        ApiManager.getInstance().getZhihuApiService().getTheDaily(date)
                .map(new Function<ZhihuDaily, ZhihuDaily>() {
                    @Override
                    public ZhihuDaily apply(@NonNull ZhihuDaily daily) throws Exception {
                        String date = daily.getDate();
                        for (ZhihuDailyItem item : daily.getStories()) {
                            item.setDate(date);
                        }
                        return daily;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ZhihuDaily>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(ZhihuDaily daily) {
                        zhihuFragment.hideProgressDialog();
                        zhihuFragment.updateList(daily);
                    }

                    @Override
                    public void onError(Throwable e) {
                        zhihuFragment.hideProgressDialog();
                        zhihuFragment.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @Override
    public void getLatestFromCache() {
        if (cacheUtil.getAsJSONObject(Config.ZHIHU) != null) {
            ZhihuDaily daily = gson.fromJson(cacheUtil.getAsJSONObject(Config.ZHIHU).toString(), ZhihuDaily.class);
            zhihuFragment.updateList(daily);
        }
    }
}
